package com.app.util.md5;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.UUID;

import com.app.util.string.StringUtil;
import com.app.util.type.BytesUtil;

public class MD5Util {

	/**
	 * 计算文件的MD5
	 * 
	 * @param file 
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws IOException
	 * @throws Exception
	 */
	public static String getMD5FromFile(File file) throws IOException {
		InputStream fis;
		fis = new FileInputStream(file);
		byte[] buffer = new byte[1024];
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			int numRead = 0;
			while ((numRead = fis.read(buffer)) > 0) {
				md5.update(buffer, 0, numRead);
			}
			fis.close();
			return BytesUtil.toHex(md5.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 计算数组的MD5
	 * 
	 * @param bs
	 * @param start
	 * @param len
	 * @return
	 * @throws IOException
	 */
	public static String getMD5FromBytes(byte[] bs, int start, int len) throws IOException {
		return getMD5FromBytes(Arrays.copyOfRange(bs, start, start + len));
	}

	/**
	 * 计算数组的MD5
	 * 
	 * @param bs
	 * @return
	 * @throws IOException
	 */
	public static String getMD5FromBytes(byte[] bs) throws IOException {
		byte[][] bss = new byte[][] { bs };
		getMD5FromBytes(bss);
		return getMD5FromBytes(bs, 0, bs.length);
	}

	/**
	 * 计算数组的MD5
	 * 
	 * @param bss
	 * @return
	 * @throws IOException
	 */
	public static String getMD5FromBytes(byte[][] bss) throws IOException {
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			for (byte[] bs : bss) {
				md5.update(bs, 0, bs.length);
			}
			return BytesUtil.toHex(md5.digest());
		} catch (NoSuchAlgorithmException e) {
		}
		return "";
	}

	/**
	 * 计算字符串的MD5
	 * 
	 * @param args
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getMD5(String args) {
		return getMD5(args, "UTF-8");

	}

	/**
	 * 计算字符串的MD5，如果(null == 字符串)，则返回null；如果(字符串.equals(""))，则返回""。
	 * 
	 * @param args
	 * @return
	 */
	public static String getMD5EmptyByEmpty(String args) {
		if (null == args) {
			return null;
		}
		if (StringUtil.isEmpty(args)) {
			return "";
		}
		return getMD5(args);
	}

	/**
	 * 计算字符串的MD5
	 * 
	 * @param args
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getMD5(String args, String encode) {
		java.security.MessageDigest alga;
		try {
			alga = java.security.MessageDigest.getInstance("MD5");
			try {
				alga.update(args.getBytes(encode));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			byte[] digesta = alga.digest();
			String str = BytesUtil.toHex(digesta); // to HexString
			return str;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 通过UUID生成MD5
	 * 
	 * @return
	 */
	public static String getMD5ByUUID() {
		return getMD5(UUID.randomUUID().toString());
	}

}
